package book.zone.controller;

import book.zone.model.CartModel;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: NDemeshchenko
 * Date: May 28, 2010
 * Time: 3:51:33 PM
 */
public class BrowseCart extends HttpServlet{
    HttpSession session;
    ArrayList<String[]> itemsInCart;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        session = req.getSession();
        if(session.getAttribute("email") == null){
            resp.sendRedirect("/signin.jsp");        
        }
        else{
            ArrayList cartItems = (ArrayList)session.getAttribute("cart");
            if (cartItems != null && cartItems.size() > 0){
                CartModel cartModel = new CartModel();
                this.itemsInCart = new ArrayList<String[]>();
                for (Object i : cartItems){
                    String [] iemDetail = cartModel.getItemsFromDatabase(Integer.parseInt((String) i));
                    itemsInCart.add(iemDetail);
                    log("inserted in itemsInCart");
                }
                session.setAttribute("itemsInCart", this.itemsInCart);
            }
            resp.sendRedirect("/cart.jsp");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
